package control;

import java.util.ArrayList;
import java.util.List;
import model.Elev;
import model.Fag;
import model.HoldFag;
import model.Potentiale;
import model.Trinmaal;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Marc
 */
public class TrinmaalHandler {

    private Session session;

    public TrinmaalHandler(Session session) {
        this.session = session;
    }

    public Trinmaal[] hentTrinmaal() {
        String hql = "from Trinmaal";
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        Trinmaal[] trinmaal = new Trinmaal[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {

            trinmaal[i] = (Trinmaal) resultList.get(i);
        }
        session.getTransaction().commit();

        return trinmaal;

    }

    public boolean erPotentiale(Elev elev, Trinmaal trinmaal) {
        boolean rtn = false;

        Object[] g = trinmaal.getPotentiales().toArray();

        for (int i = 0; i < g.length; i++) {
            if (rtn != true) {
                Potentiale pot = (Potentiale) g[i];
                if (pot.getElev().getCpr().equals(elev.getCpr())) {
                    rtn = true;
                } else {
                    rtn = false;
                }

            }
        }


        return rtn;
    }

    public Trinmaal[] hentSpecifikkeTrinmaal(Elev elev, HoldFag hf) {

        String hql = "from model.Trinmaal as T where T.fag = \'" + hf.getFag() + "\' AND T.klassetrin = " + hf.getKlassetrin();
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        ArrayList<Trinmaal> al = new ArrayList<Trinmaal>();

        for (int i = 0; i < resultList.size(); i++) {
            Trinmaal t = (Trinmaal) resultList.get(i);
            if (!erPotentiale(elev, t)) {
                al.add(t);
            }
        }

        session.getTransaction().commit();
        Trinmaal[] trinmaal = new Trinmaal[al.size()];
        for (int i = 0; i < trinmaal.length; i++) {
            trinmaal[i] = al.get(i);
        }
        return trinmaal;
    }

    public Potentiale[] hentElevsPotentialer(Elev elev, HoldFag hf) {
        Object[] objArr = elev.getPotentiales().toArray();
        ArrayList<Potentiale> arl = new ArrayList<Potentiale>();
        for (int i = 0; i < objArr.length; i++) {
            Potentiale pote = (Potentiale) objArr[i];
            if (pote.getTrinmaal().getFag() == hf.getFag() && Integer.parseInt(pote.getTrinmaal().getKlassetrin()) ==  Integer.parseInt(hf.getKlassetrin()) && !pote.isKompetence()) {
                arl.add(pote);
            }
        }

        Potentiale[] poten = new Potentiale[arl.size()];
        for (int i = 0; i < poten.length; i++) {
            poten[i] = arl.get(i);

        }
        return poten;
    }

    public Potentiale[] getElevsKompetencer(Elev elev, HoldFag hf) {
        Object[] objArr = elev.getPotentiales().toArray();
        ArrayList<Potentiale> arl = new ArrayList<Potentiale>();
        for (int i = 0; i < objArr.length; i++) {
            Potentiale pote = (Potentiale) objArr[i];
            if (pote.getTrinmaal().getFag() == hf.getFag() && Integer.parseInt(pote.getTrinmaal().getKlassetrin()) == Integer.parseInt(hf.getKlassetrin()) && pote.isKompetence()) {
                arl.add(pote);
            }
        }

        Potentiale[] poten = new Potentiale[arl.size()];
        for (int i = 0; i < poten.length; i++) {
            poten[i] = arl.get(i);

        }
        return poten;
    }
    
    public void nytTrin(Trinmaal t){
                
        session.beginTransaction();
        String hql = "from Trinmaal";
        session.flush();
        session.createQuery(hql);  
        session.save(t);
        session.getTransaction().commit();
    
    }
    
        public void sletTrin(Trinmaal trin) throws HibernateException{
                
        session.beginTransaction();
        String hql = "from Trinmaal";
        session.flush();
        session.createQuery(hql); 
        trin.getFag().getTrinmaals().remove(trin);
        session.delete(trin);
        session.getTransaction().commit();
    
    }

    public void updateTrinmaal(Trinmaal t) {
            session.beginTransaction();
        String hql = "from Trinmaal";
        session.flush();
        session.createQuery(hql);  
        session.update(t);
        session.getTransaction().commit();
    }
}
